﻿//////////////////// SOMMAIRE ///////////////////////////////////////////////////////////
/*
DISPLAY 46
DIALOG TAC_MAP
*/
////////////////////////////////////////////////////////////////////////////////////////

//////////////////// DISPLAY 46 ///////////////////////////////////////////////////////
pfunc_d46_kp = // On key pressed display 46
{
_touch = _this select 1;
_ctrl = _this select 3;
if (_touch == 20) exitWith {call pfunc_dtm_oc}; // Creation Dialog Tactical Map
if ((_touch == 57) && (_ctrl)) exitWith {call pfunc_mow}; // Espace + Ctrl = placement marker	
};

/////////////////////////////////////////////////////////////////////////////////////////

pfunc_mow = // Marker map intel set pos World pos
{
_pos = screenToWorld [0.5 , 0.5];
"marker_map_intel" setMarkerPosLocal _pos;
player groupChat "Position du marker mis à jour";

};

//////////////////// DIALOG TAC_MAP /////////////////////////////////////////////////////
pfunc_dtm_oc = // Open or Close Dialog Tactical Map IDD 20141 || Touche T
{
// if (leader group player != player) exitWith {}; // Réservé leader de groupe

if (isnull (findDisplay 20141)) then { // Creation dialog
	disableSerialization;
	
	_ok = createDialog "tac_map";
	waituntil {!isnull (findDisplay 20141)};
	_display = findDisplay 20141;
	
	// Key down
	_keyDown = _display displayAddEventHandler ["KeyDown", "_bool = _this call pfunc_dtm_kp"]; 
	
	// Map Intel
	_pos = getMarkerPos "marker_map_intel" ;
	[_pos] call pfunc_dtm_mi;
	onMapSingleClick "[_pos] call pfunc_dtm_mi";
	"marker_map_intel" setMarkerAlphaLocal 0.5;
	
	// Groupe name + list
	ctrlSetText [201413, format["%1", group player]];
	call pfunc_dtm_gl;
	
	
	// promotion
	_control = _display displayCtrl 201416;
	_control ctrlSetEventHandler ["ButtonClick", "call pfunc_dtm_pr"];

	// Draw Line
	_control = _display displayCtrl 201411;
	// _control ctrlSetEventHandler [ "Draw", "[_this] call pfunc_dtm_dl"]; 

	
	}
	
else { // fermeture dialog
	call pfunc_dtm_cd; 
	};
	
};	

pfunc_dtm_kp = // Tactical Map Key pressed
{
_touch = _this select 1;

if (_touch == 1) exitWith {call pfunc_dtm_esc}; // Touche escape
if (_touch == 19) exitWith {call pfunc_dtm_dl}; // Touche escape
};

pfunc_dtm_cd = // Close dialog
{
closeDialog 0;
onMapSingleClick "";
"marker_map_intel" setMarkerAlphaLocal 0;
};

pfunc_dtm_esc = // escape key
{
onMapSingleClick "";
"marker_map_intel" setMarkerAlphaLocal 0;
};

pfunc_dtm_mi = // MAJ map intel 
{
disableSerialization;
_pos = _this select 0;
"marker_map_intel" setMarkerPosLocal _pos;
_dist = (position player) distance _pos;
_alt = _pos select 2;
_alt = _alt * -1 + 1347; 
_grid = mapGridPosition _pos;
_dir = ((_pos select 0) - (getpos player select 0)) atan2 ((_pos select 1) - (getpos player select 1)); 
if (_dir < 0) then {_dir = _dir + 360}; 
ctrlSetText [201412, format["DISTANCE : %1 || AZIMUT : %2 || ALTITUDE : %3 || GRID : %4  ",_dist, _dir, _alt, _grid]];
};

pfunc_dtm_gl = // Groupe list
{
_units = units group player;
_leader = leader group player;
lbClear 201414;
{
_name = name _x;
if (_x == _leader) then {_name = format["** %1 **", name _x]};
lbAdd [201414, _name];
} forEach _units;

};

pfunc_dtm_pr = // Promotion
{
_index = lbCurSel 201414;
if (_index == -1) exitWith {hint "lol"};
_units = units group player;
_unit = _units select _index;
group player selectLeader _unit; 
_name = name _unit;
player groupChat format["%1 prend le commandement du groupe %2", _name, group player];
call pfunc_dtm_cd;
};
	
pfunc_dtm_dl = // Draw Line
{
disableSerialization;
_display = findDisplay 20141;
_control = _display displayCtrl 201411;
_pos = getPos player;
_pos2 = getMarkerPos "title_A";
hint "ok";
_ok = _control drawLine [_pos, _pos2, [1,1,0,1]];
hint str(_ok);
};


////////////////////////////////////////////////////////////////////////////////////////


//////////////////// EVENT HANDLER ////////////////////////////////////////////////////
pfunc_eh_killed = // Killed
{
	disableSerialization;
	_ar = _this select 0;
	_unit = _ar select 0;
	if (isPlayer _unit) then {
		sleep 2;
		
		// Création du display
		1 cutRsc ["lg_ui_ds", "BLACK", 0];
		while {!alive player} do {
			((uiNamespace getVariable "ui_ds") displayCtrl 201432) ctrlSetText ".";
			if (!alive player) then {sleep 1};
			((uiNamespace getVariable "ui_ds") displayCtrl 201432) ctrlSetText "..";
			if (!alive player) then {sleep 1};
			((uiNamespace getVariable "ui_ds") displayCtrl 201432) ctrlSetText "...";
			if (!alive player) then {sleep 1};
		};
		((uiNamespace getVariable "ui_ds") displayCtrl 201432) ctrlSetText "Bonne chance";
	};
		
};

pfunc_eh_respawn = // Respawn
{	
	_ar = _this select 0;
	_unit = _ar select 0;
	_corpse = _ar select 1;
	
	call pfunc_eh_remove;
	
	_side = side (group _unit);
	
	[grunt, "grunt"] call pfunc_new_model;	
	
	
	
	player enableSimulation false;
	0 cutRsc ["empty", "BLACK FADED", 1];
	sleep 3;
	1 cutRsc ["empty", "PLAIN", 1];
	player enableSimulation true;
};



pfunc_eh_remove = // Remove EH
{
	player removeEventHandler ["respawn", eh_respawn];
	player removeEventHandler ["killed", eh_killed];
	
};

pfunc_eh_add = // Add EH
{
	eh_killed = player addEventhandler ["killed", {[_this] spawn pfunc_eh_killed}];
	eh_respawn = player addEventhandler ["respawn", {[_this] spawn pfunc_eh_respawn}];
	
};


////////////////////////////////////////////////////////////////////////////////////////


//////////////////// UNIT /////////////////////////////////////////////////////////////
pfunc_new_model = // Changer de model de soldat
{
	_model = _this select 0;
	_kit = _this select 1;
	_group = createGroup p_side;
	_old = vehicle player;
	_new = _group createUnit [_model, respawn_pos, [], 0, "NONE"];
	setPlayable _new;
	selectPlayer _new;
	_kit = format["\lg_main\kit\%1\%2.sqf", lg_tag, _kit];
	[] execVM _kit;
	[player] join g_reserve;
	deleteVehicle _old;
	deleteGroup _group;
	call pfunc_eh_add;
	player addAction ["test", "test.sqf"];
};
////////////////////////////////////////////////////////////////////////////////////////


